<?php
/**
 * Description of ShoppingUSPSShipping
 *
 * @author Pavel Kovalyov
 */
class RCMS_Object_Shipping_Services_USPS {

    protected $_server = 'http://Production.ShippingAPIs.com/ShippingAPI.dll';
    
    protected $_username;
    protected $_userId;
    protected $_password;

    protected $_destZip;
    protected $_origZip;

    protected $_packageWeight = array (
        'pounds' => '',
        'ounces' => '',
    );
    protected $_packageSize = array (
        'lenght' => '',
        'width'  => '',
        'height' => '',
    );
    protected $_packageContainerId;

    public function setServer($server) {
        $this->_server = $server;
    }

    public function setUserId($userId){
        $this->_userId = $userId;
    }

    public function getUserId(){
        return $this->_userId;
    }

    public function setPassword($password) {
        $this->_password = $password;
    }

    public function getPassword() {
        return $this->_password;
    }

    public function setUsername($username) {
        $this->_username = $username;
    }

    public function getAccessKey() {
        return $this->_username;
    }

    public function setWeight($pounds, $ounces) {
        $this->_packageWeight = array(
            'pounds' => $pounds,
            'ounces' => $ounces
            );
    }

    public function getWeight(){
        return $this->_packageWeight;
    }

    public function getShippingPrice() {
        $query  = 'API=RateV3&XML=';
        $query .= '<RateV3Request USERID="'.$this->_userId.'">';
        $query .= '<Package ID="1ST">';
        $query .= "<Service>PRIORITY</Service><ZipOrigination>$this->_origZip</ZipOrigination><ZipDestination>$this->_destZip</ZipDestination><Pounds>$this->_packageWeight['pounds']</Pounds><Ounces>$this->_packageWeight['ounces']</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable></Package></RateV3Request>";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_server);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($query));

        $result = curl_exec($ch);
        curl_close($ch);
    }
}

